package com.cskaoyan.javase.practice.stage8.utils;

import com.cskaoyan.javase.practice.stage8.annos.*;
import com.cskaoyan.javase.practice.stage8.entity.Student;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * @program: little-practice
 * @description:  test whether field value is legal according to its annotation.
 * @author: Keyu Li
 * @create: 2021-09-20 16:36
 **/

public class AnnoUtils {
    // 工具类私有化构造方法
    private AnnoUtils() {
    }

    // student类全限定类名
    static String studentReference = "com.cskaoyan.javase.practice.stage8.entity.Student";


    // 获取名字注解，判断合法
    public static boolean checkLegalName(String name)
            throws ClassNotFoundException, NoSuchFieldException {
        Class<?> stuClass = Class.forName(studentReference);
        Field nameField = stuClass.getDeclaredField("name");
        if (nameField.isAnnotationPresent(NameAnno.class)) {
            NameAnno nameAnno = nameField.getAnnotation(NameAnno.class);
            return name.length() <= nameAnno.nameLimit();
        }
        return false;
    }

    // 获取年龄注解，判断合法
    public static boolean checkLegalAge(int age) throws ClassNotFoundException, NoSuchFieldException {
        Class<?> stuClass = Class.forName(studentReference);
        Field ageField = stuClass.getDeclaredField("age");
        if (ageField.isAnnotationPresent(AgeAnno.class)) {
            AgeAnno ageAnno = ageField.getAnnotation(AgeAnno.class);
            int minAge = ageAnno.minAge();
            int maxAge = ageAnno.maxAge();
            return age >= minAge && age <= maxAge;
        }
        return false;
    }


    // 获取性别注解，判断合法
    public static boolean checkLegalGender(String gender) throws ClassNotFoundException, NoSuchFieldException {
        Class<?> stuClass = Class.forName(studentReference);
        Field genderField = stuClass.getDeclaredField("gender");
        if (genderField.isAnnotationPresent(GenderAnno.class)) {
            GenderAnno genderAnno = genderField.getAnnotation(GenderAnno.class);
            String[] genders = genderAnno.gender();
            for (int i = 0; i < genders.length; i++) {
                if (gender.equals(genders[i]))
                    return true;
            }
        }
        return false;
    }

    // 获取电话注解，判断合法
    public static boolean checkLegalTele(String tele) throws ClassNotFoundException, NoSuchFieldException {
        Class<?> stuClass = Class.forName(studentReference);
        Field phoneField = stuClass.getDeclaredField("tele");
        if (phoneField.isAnnotationPresent(PhoneAnno.class)) {
            PhoneAnno phoneAnno = phoneField.getAnnotation(PhoneAnno.class);
            return phoneAnno.phoneLength() == tele.length();
        }
        return false;
    }

    // 获取年纪注解，判断合法
    public static boolean checkLegalGrade(String grade) throws ClassNotFoundException, NoSuchFieldException {
        Class<?> stuClass = Class.forName(studentReference);
        Field gradeField = stuClass.getDeclaredField("grade");
        if (gradeField.isAnnotationPresent(GradeAnno.class)) {
            GradeAnno gradeAnno = gradeField.getAnnotation(GradeAnno.class);
            String[] grades = gradeAnno.grade();
            for (int i = 0; i < grades.length; i++) {
                if (grade.equals(grades[i]))
                    return true;
            }
        }
        return false;
    }

}
